home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / midi / midifl12.lha / midifile.new / mftext.c < prev    next >
C/C++ Source or Header  |  1995-08-27  |  9KB  |  481 lines

  1. /*
  2.  * mftext
  3.  * 
  4.  * Convert a MIDI file to verbose text.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "midifile.h"
  10.  
  11. static FILE *F;
  12. int SECONDS;            /* global that tells whether to display seconds or ticks */
  13. int division;            /* from the file header */
  14. long tempo = 500000;        /* the default tempo is 120 beats/minute */
  15.  
  16. /* filegetc : returns <int> with input variables:
  17.  * void:
  18.  */
  19. int filegetc (void)
  20. {
  21.   return (getc (F));
  22. }
  23.  
  24. /* ------------------------------------------------------------------------ */
  25.  
  26. /* for crack */
  27. extern int arg_index;
  28.  
  29. /* main : returns <int> with input variables:
  30.  * argc:
  31.  * argv:
  32.  */
  33. int main (
  34.   int argc,
  35.   char **argv)
  36. {
  37.   FILE *efopen ();
  38.   char ch;
  39.  
  40.   SECONDS = 0;
  41.  
  42.   while ((ch = crack (argc, argv, "s", 0)) != NULL) {
  43.  
  44.     switch (ch) {
  45.  
  46.         case 's':
  47.           SECONDS = 1;
  48.           break;
  49.         }
  50.     }
  51.  
  52.   if (argc < 2 && !SECONDS || argc < 3 && SECONDS)
  53. F = stdin;
  54.   else
  55. F = efopen (argv[arg_index], "r");
  56.  
  57.   initfuncs ();
  58.   Mf_getc = filegetc;
  59.   midifile ();
  60.   fclose (F);
  61.   exit (0);
  62. }
  63.  
  64. /* ------------------------------------------------------------------------ */
  65.  
  66. /* * efopen : returns <FILE> with input variables:
  67.  * name:
  68.  * mode:
  69.  */
  70. FILE * efopen (
  71.   char *name,
  72.   char *mode)
  73. {
  74.   FILE *f;
  75.   extern int errno;
  76.  
  77.  
  78.   char *errmess;
  79.  
  80.   if ((f = fopen (name, mode)) == NULL) {
  81.  
  82.     (void) fprintf (stderr, "*** ERROR *** Cannot open '%s'!\n", name);
  83.     exit (1);
  84.     }
  85.   return (f);
  86. }
  87.  
  88. /* ------------------------------------------------------------------------ */
  89.  
  90. /* error : returns <int> with input variables:
  91.  * s:
  92.  */
  93. int error (char *s)
  94. {
  95.   fprintf (stderr, "Error: %s\n", s);
  96. }
  97.  
  98. /* ------------------------------------------------------------------------ */
  99.  
  100. /* txt_header : returns <int> with input variables:
  101.  * format:
  102.  * ntrks:
  103.  * ldivision:
  104.  */
  105. int txt_header (
  106.   int format,
  107.   int ntrks,
  108.   int ldivision)
  109. {
  110.   division = ldivision;
  111.   printf ("Header format=%d ntrks=%d division=%d\n", format, ntrks, division);
  112. }
  113.  
  114. /* ------------------------------------------------------------------------ */
  115.  
  116. /* txt_trackstart : returns <int> with input variables:
  117.  * void:
  118.  */
  119. int txt_trackstart (void)
  120. {
  121.   printf ("Track start\n");
  122. }
  123.  
  124. /* ------------------------------------------------------------------------ */
  125.  
  126. /* txt_trackend : returns <int> with input variables:
  127.  * void:
  128.  */
  129. int txt_trackend (void)
  130. {
  131.   printf ("Track end\n");
  132. }
  133.  
  134. /* ------------------------------------------------------------------------ */
  135.  
  136. /* txt_noteon : returns <int> with input variables:
  137.  * chan:
  138.  * pitch:
  139.  * vol:
  140.  */
  141. int txt_noteon (
  142.   int chan,
  143.   int pitch,
  144.   int vol)
  145. {
  146.   prtime ();
  147.   printf ("Note on, chan=%d pitch=%d vol=%d\n", chan + 1, pitch, vol);
  148. }
  149.  
  150. /* ------------------------------------------------------------------------ */
  151.  
  152. /* txt_noteoff : returns <int> with input variables:
  153.  * chan:
  154.  * pitch:
  155.  * vol:
  156.  */
  157. int txt_noteoff (
  158.   int chan,
  159.   int pitch,
  160.   int vol)
  161. {
  162.   prtime ();
  163.   printf ("Note off, chan=%d pitch=%d vol=%d\n", chan + 1, pitch, vol);
  164. }
  165.  
  166. /* ------------------------------------------------------------------------ */
  167.  
  168. /* txt_pressure : returns <int> with input variables:
  169.  * chan:
  170.  * pitch:
  171.  * press:
  172.  */
  173. int txt_pressure (
  174.   int chan,
  175.   int pitch,
  176.   int press)
  177. {
  178.   prtime ();
  179.   printf ("Pressure, chan=%d pitch=%d press=%d\n", chan + 1, pitch, press);
  180. }
  181.  
  182. /* ------------------------------------------------------------------------ */
  183.  
  184. /* txt_parameter : returns <int> with input variables:
  185.  * chan:
  186.  * control:
  187.  * value:
  188.  */
  189. int txt_parameter (
  190.   int chan,
  191.   int control,
  192.   int value)
  193. {
  194.   prtime ();
  195.   printf ("Parameter, chan=%d c1=%d c2=%d\n", chan + 1, control, value);
  196. }
  197.  
  198. /* ------------------------------------------------------------------------ */
  199.  
  200. /* txt_pitchbend : returns <int> with input variables:
  201.  * chan:
  202.  * msb:
  203.  * lsb:
  204.  */
  205. int txt_pitchbend (
  206.   int chan,
  207.   int msb,
  208.   int lsb)
  209. {
  210.   prtime ();
  211.   printf ("Pitchbend, chan=%d msb=%d lsb=%d\n", chan + 1, msb, lsb);
  212. }
  213.  
  214. /* ------------------------------------------------------------------------ */
  215.  
  216. /* txt_program : returns <int> with input variables:
  217.  * chan:
  218.  * program:
  219.  */
  220. int txt_program (
  221.   int chan,
  222.   int program)
  223. {
  224.   prtime ();
  225.   printf ("Program, chan=%d program=%d\n", chan + 1, program);
  226. }
  227.  
  228. /* ------------------------------------------------------------------------ */
  229.  
  230. /* txt_chanpressure : returns <int> with input variables:
  231.  * chan:
  232.  * press:
  233.  */
  234. int txt_chanpressure (
  235.   int chan,
  236.   int press)
  237. {
  238.   prtime ();
  239.   printf ("Channel pressure, chan=%d pressure=%d\n", chan + 1, press);
  240. }
  241.  
  242. /* ------------------------------------------------------------------------ */
  243.  
  244. /* txt_sysex : returns <int> with input variables:
  245.  * leng:
  246.  * mess:
  247.  */
  248. int txt_sysex (
  249.   int leng,
  250.   char *mess)
  251. {
  252.   prtime ();
  253.   printf ("Sysex, leng=%d\n", leng);
  254. }
  255.  
  256. /* ------------------------------------------------------------------------ */
  257.  
  258. /* txt_metamisc : returns <int> with input variables:
  259.  * type:
  260.  * leng:
  261.  * mess:
  262.  */
  263. int txt_metamisc (
  264.   int type,
  265.   int leng,
  266.   char *mess)
  267. {
  268.   prtime ();
  269.   printf ("Meta event, unrecognized, type=0x%02x leng=%d\n", type, leng);
  270. }
  271.  
  272. /* ------------------------------------------------------------------------ */
  273.  
  274. /* txt_metaspecial : returns <int> with input variables:
  275.  * type:
  276.  * leng:
  277.  * mess:
  278.  */
  279. int txt_metaspecial (
  280.   int type,
  281.   int leng,
  282.   char *mess)
  283. {
  284.   prtime ();
  285.   printf ("Meta event, sequencer-specific, type=0x%02x leng=%d\n", type, leng);
  286. }
  287.  
  288. /* ------------------------------------------------------------------------ */
  289.  
  290. /* txt_metatext : returns <int> with input variables:
  291.  * type:
  292.  * leng:
  293.  * mess:
  294.  */
  295. int txt_metatext (
  296.   int type,
  297.   int leng,
  298.   char *mess)
  299. {
  300.   static char *ttype[] =
  301.   {
  302.     NULL,
  303.     "Text Event",        /* type=0x01 */
  304.     "Copyright Notice",        /* type=0x02 */
  305.     "Sequence/Track Name",
  306.     "Instrument Name",        /* ...       */
  307.     "Lyric",
  308.     "Marker",
  309.     "Cue Point",        /* type=0x07 */
  310.     "Unrecognized"
  311.       }
  312. ;
  313.   int unrecognized = (sizeof (ttype) / sizeof (char *)) - 1;
  314.   register int n, c;
  315.   register char *p = mess;
  316.  
  317.   if (type < 1 || type > unrecognized)
  318. type = unrecognized;
  319.   prtime ();
  320.   printf ("Meta Text, type=0x%02x (%s)  leng=%d\n", type, ttype[type], leng);
  321.   printf ("     Text = <");
  322.   for (n = 0; n < leng; n++) {
  323.  
  324.     c = *p++;
  325.     printf ((isprint (c) || isspace (c)) ? "%c" : "\\0x%02x", c);
  326.     }
  327.   printf (">\n");
  328. }
  329.  
  330. /* ------------------------------------------------------------------------ */
  331.  
  332. /* txt_metaseq : returns <int> with input variables:
  333.  * num:
  334.  */
  335. int txt_metaseq (int num)
  336. {
  337.   prtime ();
  338.   printf ("Meta event, sequence number = %d\n", num);
  339. }
  340.  
  341. /* ------------------------------------------------------------------------ */
  342.  
  343. /* txt_metaeot : returns <int> with input variables:
  344.  * void:
  345.  */
  346. int txt_metaeot (void)
  347. {
  348.   prtime ();
  349.   printf ("Meta event, end of track\n");
  350. }
  351.  
  352. /* ------------------------------------------------------------------------ */
  353.  
  354. /* txt_keysig : returns <int> with input variables:
  355.  * sf:
  356.  * mi:
  357.  */
  358. int txt_keysig (
  359.   int sf,
  360.   int mi)
  361. {
  362.   prtime ();
  363.   printf ("Key signature, sharp/flats=%d  minor=%d\n", sf, mi);
  364. }
  365.  
  366. /* ------------------------------------------------------------------------ */
  367.  
  368. /* txt_tempo : returns <int> with input variables:
  369.  * ltempo:
  370.  */
  371. int txt_tempo (long ltempo)
  372. {
  373.   tempo = ltempo;
  374.   prtime ();
  375.   printf ("Tempo, microseconds-per-MIDI-quarter-note=%d\n", tempo);
  376. }
  377.  
  378. /* ------------------------------------------------------------------------ */
  379.  
  380. /* txt_timesig : returns <int> with input variables:
  381.  * nn:
  382.  * dd:
  383.  * cc:
  384.  * bb:
  385.  */
  386. int txt_timesig (
  387.   int nn,
  388.   int dd,
  389.   int cc,
  390.   int bb)
  391. {
  392.   int denom = 1;
  393.   while (dd-- > 0)
  394. denom *= 2;
  395.   prtime ();
  396.   printf ("Time signature=%d/%d  MIDI-clocks/click=%d  32nd-notes/24-MIDI-clocks=%d\n",
  397.   nn, denom, cc, bb);
  398. }
  399.  
  400. /* ------------------------------------------------------------------------ */
  401.  
  402. /* txt_smpte : returns <int> with input variables:
  403.  * hr:
  404.  * mn:
  405.  * se:
  406.  * fr:
  407.  * ff:
  408.  */
  409. int txt_smpte (
  410.   int hr,
  411.   int mn,
  412.   int se,
  413.   int fr,
  414.   int ff)
  415. {
  416.   prtime ();
  417.   printf ("SMPTE, hour=%d minute=%d second=%d frame=%d fract-frame=%d\n",
  418.   hr, mn, se, fr, ff);
  419. }
  420.  
  421. /* ------------------------------------------------------------------------ */
  422.  
  423. /* txt_arbitrary : returns <int> with input variables:
  424.  * leng:
  425.  * mess:
  426.  */
  427. int txt_arbitrary (
  428.   int leng,
  429.   char *mess)
  430. {
  431.   prtime ();
  432.   printf ("Arbitrary bytes, leng=%d\n", leng);
  433. }
  434.  
  435. /* ------------------------------------------------------------------------ */
  436.  
  437. /* prtime : returns <int> with input variables:
  438.  * void:
  439.  */
  440. int prtime (void)
  441. {
  442.   if (SECONDS)
  443. printf ("Time=%f   ", mf_ticks2sec (Mf_currtime, division, tempo));
  444.   else
  445. printf ("Time=%ld  ", Mf_currtime);
  446. }
  447.  
  448. /* ------------------------------------------------------------------------ */
  449.  
  450. /* initfuncs : returns <int> with input variables:
  451.  * void:
  452.  */
  453. int initfuncs (void)
  454. {
  455.   Mf_error = error;
  456.   Mf_header = txt_header;
  457.   Mf_trackstart = txt_trackstart;
  458.   Mf_trackend = txt_trackend;
  459.   Mf_noteon = txt_noteon;
  460.   Mf_noteoff = txt_noteoff;
  461.   Mf_pressure = txt_pressure;
  462.   Mf_parameter = txt_parameter;
  463.   Mf_pitchbend = txt_pitchbend;
  464.   Mf_program = txt_program;
  465.   Mf_chanpressure = txt_chanpressure;
  466.   Mf_sysex = txt_sysex;
  467.   Mf_metamisc = txt_metamisc;
  468.   Mf_seqnum = txt_metaseq;
  469.   Mf_eot = txt_metaeot;
  470.   Mf_timesig = txt_timesig;
  471.   Mf_smpte = txt_smpte;
  472.   Mf_tempo = txt_tempo;
  473.   Mf_keysig = txt_keysig;
  474.   Mf_seqspecific = txt_metaspecial;
  475.   Mf_text = txt_metatext;
  476.   Mf_arbitrary = txt_arbitrary;
  477. }
  478.  
  479. /* ------------------------------------------------------------------------ */
  480.  
  481.